package com.bd.rj.frontstage.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.bd.rj.common.entity.Resume;
import com.bd.rj.frontstage.service.UploadImageService;
import com.bd.rj.frontstage.service.impl.ResumeServiceImpl;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.Map;

import static com.baomidou.mybatisplus.core.toolkit.Wrappers.update;

@RestController
@RequestMapping("/resume")
public class ResumeController {

    @Resource
    ResumeServiceImpl resumeService;

    @Resource
    UploadImageService uploadImageService;
    /**
     * 查看用户简历
     * @author 高瑾楠
     * @param
     * @return
     */
    @GetMapping("/queryresume")
    public Map<String,Object> queryresume(Long uid) {
        Map<String,Object> map = new HashMap<>();
        QueryWrapper<Resume> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("uid",uid);
        map.put("code",200);
        map.put("msg","查询成功");
        map.put("data",resumeService.list(queryWrapper));
        return map;
    }

    /**
     * 修改用户简历
     * @author 高瑾楠
     */
    @RequestMapping(value = "/update",method = RequestMethod.POST)
    public Map<String, Object> updateResume(Resume resume) {
        System.out.println("ResumeController -> updateResume");
        Map<String, Object> map = new HashMap<>();
        boolean result = resumeService.updateById(resume);
        if (result) {
            map.put("code", 200);
            map.put("msg", "修改成功！");
            map.put("data", "更新成功");
        } else {
            map.put("code", 500);
            map.put("msg", "修改失败");
            map.put("data", "更新失败");
        }
        return map;
    }

    /**
     * @desc   添加简历
     * @author yxh
     * @timedate
     */
    @RequestMapping(value = "/uploadAndInsertResume", method = RequestMethod.POST)
    public Map<String, Object> uploadAndInsertResume(
            @RequestBody MultipartFile file,
            @RequestParam("uid") Long uid,
            @RequestParam("educational") String educational,
            @RequestParam("expectation") String expectation,
            @RequestParam("skill") String skill,
            @RequestParam(value = "detail", required = false) String detail
    ) {
        Map<String, Object> map = new HashMap<>();

        // 1. 如果有文件上传，则处理文件上传逻辑
        String filePath = null;
        if (file != null && !file.isEmpty()) {
            filePath = uploadImageService.uploadQNFile(file, "resume/");
            if (filePath.equals("失败")) {
                map.put("code", 201);
                map.put("msg", "文件上传失败");
                map.put("data", null);
                return map;
            } else {
                System.out.println("七牛云附件上传成功: " + filePath);
            }
        }

        // 2. 添加简历
        Resume resume = new Resume();
        resume.setUid(uid);
        resume.setEducational(educational);
        resume.setExpectation(expectation);
        resume.setSkill(skill);
        resume.setDetail(detail);
        resume.setUrl(filePath);

        boolean isSaved = resumeService.save(resume);
        if (isSaved) {
            map.put("code", 200);
            map.put("msg", "简历添加成功");
            map.put("data", resume);
        } else {
            map.put("code", 202);
            map.put("msg", "简历添加失败");
            map.put("data", null);
        }

        return map;
    }

    /**
     * @desc   上传附件
     * @author yxh
     * @timedate
     */
    @RequestMapping(value = "/upload", method = RequestMethod.POST)
    public Map<String, Object> uploadAttachment(@RequestParam("file") MultipartFile file, @RequestParam("uid") Long uid) {
        Map<String, Object> map = new HashMap<>();
        if (!file.isEmpty()) {
            // 参数1: 上传的文件
            // 参数2: 文件要上传至的服务器文件夹
            String path = uploadImageService.uploadQNFile(file, "resume/");
            if (path.equals("失败")) {
                map.put("code", 201);
                map.put("msg", "上传失败");
                map.put("data", file);
            } else {
                System.out.println("七牛云附件上传成功" + path);
                map.put("code", 200);
                map.put("msg", "上传成功");

                map.put("data", path);

                // 根据uid更新简历的url字段
                Resume resume = new Resume();
                resume.setUrl(path);
                QueryWrapper<Resume> queryWrapper = new QueryWrapper<>();
                queryWrapper.eq("uid", uid);
                boolean rows =resumeService.update(resume, queryWrapper);
                System.out.println("数据库更新结果: " + rows);
            }
        } else {
            map.put("code", 202);
            map.put("msg", "文件为空");
            map.put("data", null);
        }
        return map;
    }
}